<template>
  <el-dialog title="备份数据库" class="dia-style" :visible.sync="open" width="500px" append-to-body>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button :disabled="!isSelected" type="primary" size="mini" @click="handlePackage">{{
          buttonText
        }}</el-button>
      </el-col>
    </el-row>
    <HkTable
      :tableData="tableData"
      :tableColumns="tableColumns"
      :showIndex="true"
      :selection="true"
      :showPag="false"
      :table-height="80"
      @handleSelectionChange="handleSelectionChange">
    </HkTable>
  </el-dialog>
</template>

<script>
import HkTable from '@/components/HKTable/index.vue'
import { getDatabaseList, mysqlBackups } from '@/api/recovery/index'

export default {
  components: { HkTable },
  data() {
    return {
      //  弹窗控制器
      open: false,
      // 选中的id值们
      ids: '',
      //  是否选中
      isSelected: false,
      //  按钮文本
      buttonText: '备份',
      //  table数据
      tableData: [],
      //  table配置项
      tableColumns: [{ label: '数据库名称', param: 'name' }],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    /** 获取数据库列表 */
    getList() {
      getDatabaseList().then((res) => {
        this.tableData = res.map(key => ({ name: key, id: key }))
      })
    },
    /** 表格选中事件 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.isSelected = selection.length > 0
      this.buttonText = selection.length < 2 ? '备份' : '批量备份'
    },

    /** 执行备份 */
    handlePackage() {
      mysqlBackups(this.ids).then((res) => {
        this.$message.success('备份成功')
        this.$emit('handleBackup')
        this.open = false
      })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body{
  max-height:80vh;
  overflow:hidden;
  display:flex;
  flex-direction: column;
}
.targetTable{
  flex:1
}
::v-deep .el-table__body-wrapper{
  overflow:auto !important;
}
button.el-button.el-button--primary.is-disabled,
button.el-button.el-button--primary.is-disabled:active {
  color: #ffffff !important;
  background-color: #a5b7f7 !important;
  border-color: #a5b7f7 !important;
}
</style>
